package com.panshi.controller;

import com.panshi.service.ITitleService;
import com.panshi.vo.qa.DataResultQA;
import com.panshi.vo.qa.Title;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("分类业务接口")
@Slf4j
@Controller
@RequestMapping(value = "/title", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TitleController {

    @Autowired
    ITitleService titleService;

    //添加分类
    @PostMapping(value = "/add")
    @ResponseBody
    @ApiOperation("添加分类")
    @ApiImplicitParam(name = "title",value = "title:{tname,tstate,type}")
    public DataResultQA addTitle(@RequestBody Title title){
        log.info("请求路径：{},参数：{}","/title/add",title);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = title.getTname() == null || title.getType() == 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = titleService.addTitle(title);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("分类添加完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("分类添加失败！");
        }
        return dataResultQA;
    }

    //查询分类
    @PostMapping(value = "/queryTitle")
    @ResponseBody
    @ApiOperation("查询分类")
    @ApiImplicitParam(name = "title",value = "查询条件")
    public DataResultQA<List<Title>> queryTitle(@RequestBody Title title){
        log.info("请求路径：{},参数：{}","/title/queryTitle",title);
        DataResultQA<List<Title>> dataResultQA = new DataResultQA<>();
        boolean b = title.getTstate() == 0 || title.getType() == 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        List<Title> lsit = titleService.queryTitle(title);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询分类！");
        dataResultQA.setData(lsit);
        return dataResultQA;
    }

    //查询分类
    @PostMapping(value = "/queryByNameTitle")
    @ResponseBody
    @ApiOperation("查询单个分类")
    @ApiImplicitParam(name = "title",value = "查询条件")
    public DataResultQA<Title> queryByNameTitle(@RequestBody Title title){
        log.info("请求路径：{},参数：{}","/title/queryByNameTitle",title);
        DataResultQA<Title> dataResultQA = new DataResultQA<>();
        boolean b = title.getTname() == null || title.getType() == 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        Title t = titleService.queryByNameTitle(title);
        List<Title> lsit = titleService.queryTitle(title);
        dataResultQA.setCode(200);
        dataResultQA.setBoo(true);
        dataResultQA.setDescribe("查询分类！");
        dataResultQA.setData(t);
        return dataResultQA;
    }

    //修改分类
    @PostMapping(value = "/updateTitle")
    @ResponseBody
    @ApiOperation("修改分类")
    @ApiImplicitParam(name = "title",value = "title:{tname,type}")
    public DataResultQA updateTitle(@RequestBody Title title){
        log.info("请求路径：{},参数：{}","/title/updateTitle",title);
        DataResultQA dataResultQA = new DataResultQA();
        boolean b = title.getTname() == null || title.getType() == 0;
        if(b){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = titleService.updateTitle(title);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("点赞加一完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("点赞加一失败！");
        }
        return dataResultQA;
    }

    //禁用分类

    @PostMapping(value = "/forbidUseTitle/{tid}")
    @ResponseBody
    @ApiOperation("禁用分类")
    @ApiImplicitParam(name = "tid",value = "分类id")
    public DataResultQA forbidUseTitle(@PathVariable int tid){
        log.info("请求路径：{},参数：{}","/title/forbidUseTitle/{tid}",tid);
        DataResultQA dataResultQA = new DataResultQA();
        if(tid <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = titleService.forbidUseTitle(tid);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("禁用分类完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("禁用分类失败！");
        }
        return dataResultQA;
    }

    //启用分类
    @PostMapping(value = "/startUseTitle/{tid}")
    @ResponseBody
    @ApiOperation("启用分类")
    @ApiImplicitParam(name = "tid",value = "分类id")
    public DataResultQA startUseTitle(@PathVariable int tid){
        log.info("请求路径：{},参数：{}","/title/startUseTitle/{tid}",tid);
        DataResultQA dataResultQA = new DataResultQA();
        if(tid <= 0){
            dataResultQA.setCode(110);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("参数不正确！");
            return dataResultQA;
        }
        boolean boo = titleService.startUseTitle(tid);
        if(boo){
            dataResultQA.setCode(200);
            dataResultQA.setBoo(true);
            dataResultQA.setDescribe("启用分类完成！");
        }else{
            dataResultQA.setCode(120);
            dataResultQA.setBoo(false);
            dataResultQA.setDescribe("启用分类失败！");
        }
        return dataResultQA;
    }
}
